/****************************************************/
/****************************************************/
/*** PART 5: APPENDIX TABLES - ROBUSTNESS CHECKS  ***/
/****************************************************/
/****************************************************/

	** INTERACTION VARIABLE - TEACHER'S IAT AND REFUGEE DUMMY

	ge TBIAS_refugee=biat_ethnic*refugee

	global covar2a TBIAS_refugee i.refugee##i.male i.refugee##c.ageinm i.refugee##c.braven_sd i.refugee##c.beyes_sd i.refugee##i.astudent i.refugee##c.s_ref_exposure

	** DEFINE GLOBALS FOR DEPENDENT VARIABLES 

	global network_ties bnode_in_friend support_received support_provided study_received study_provided
	global host_refugee_ties bfriend_total_refugee bfriend_total_host bsupport_total_refugee bsupport_total_host bsupport_self_total_refugee bsupport_self_total_host bstudy_total_refugee bstudy_total_host bstudy_self_total_refugee bstudy_self_total_host
	global achievement bturk_std bmath_std


******************************************************
*** CLASS FE
*******************************************************

	local i = 1
	foreach depvar in $network_ties $achievement bsbully{
	qui reg `depvar'  $covar2a i.b_classid if reg_sample==1, cluster(b_classid)
					  est store est`i'
	qui reg `depvar'  biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1
	estimates store m1
	qui reg `depvar'  biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1
	estimates store m2
	suest m1 m2, cluster(b_classid)
	lincom [m2_mean]biat_ethnic-[m1_mean]biat_ethnic
	estadd scalar p1=r(estimate),  : est`i'
	estadd scalar p2=r(se),  : est`i'
	estadd scalar p3=r(p),  : est`i'			  
					  
					  
	local i = `i'+1
	}


	probit d_bsbully $covar2a i.b_classid if sample1==1, cluster(b_classid)
	margins, dydx(TBIAS_refugee) post
					  est store est9
					  
				  
	reg d_bsbully biat_ethnic $covar1 i.grade1 i.b_schoolid if refugee==0 & sample1==1
	estimates store m1
	reg d_bsbully biat_ethnic $covar1 i.grade1 i.b_schoolid if refugee==1 & sample1==1
	estimates store m2
	suest m1 m2, cluster(b_classid)
	lincom [m2_mean]biat_ethnic-[m1_mean]biat_ethnic
	estadd scalar p1=r(estimate),  : est9
	estadd scalar p2=r(se),  : est9
	estadd scalar p3=r(p),  : est9



		#delimit ;
		esttab est1 est2 est3 est4 est5
		using "$path2/appendix_tableA5a.tex",  
		keep(TBIAS_refugee) 
			replace compress b(%8.2f) se(2)  noconstant nonumbers nobaselevels		
		coeflabels(TBIAS_refugee "Teacher Ethnic Bias*Refugee")		
	
		star(* 0.10 ** 0.05 *** 0.01) nonotes 
		s(p1 p2 p3 N, fmt(%8.2f %8.2f  %8.3f %9.0g) labels("Main Specification (Refugee-Host)" "Standard Error" "P-value" "\hline N"))
		nogaps		
		
		mtitles("Friendship" "E.S. Received" "E.S. Provided" "A.S. Received" "A.S. Provided")


		;
		#delimit cr	

		
		#delimit ;
		esttab est6 est7 est9 est8 
		using "$path2/appendix_tableA5b.tex",  
		keep(TBIAS_refugee) 
			replace compress b(%8.2f) se(2)  noconstant nonumbers nobaselevels		
		coeflabels(TBIAS_refugee "Teacher Ethnic Bias*Refugee")		
	
		star(* 0.10 ** 0.05 *** 0.01) nonotes 
		s(p1 p2 p3 N, fmt(%8.2f %8.2f  %8.3f %9.0g) labels("Main Specification (Refugee-Host)" "Standard Error" "P-value" "\hline N"))
		nogaps		
		
		mtitles("Turkish" "Mathematics" "Probability Bullied" "Number of  Bullies")
		;
		#delimit cr	

		
******************************************************
*** NO TEACHER CHARACTERISTICS
*******************************************************

	global covar2c  male ageinm braven_sd beyes_sd astudent s_ref_exposure

	local i = 1
	foreach depvar in $network_ties $achievement bsbully{
	qui reg `depvar'  biat_ethnic $covar2c i.grade1 i.b_schoolid if refugee==0 & reg_sample==1, cluster(b_classid)
	estimates store spec`i'a
	qui reg `depvar' biat_ethnic $covar2c i.grade1 i.b_schoolid  if refugee==1 & reg_sample==1, cluster(b_classid)
	estimates store spec`i'b
	qui reg `depvar'  biat_ethnic $covar2c i.grade1 i.b_schoolid if refugee==0 & reg_sample==1
	estimates store m1
	qui reg `depvar'  biat_ethnic $covar2c i.grade1 i.b_schoolid if refugee==1 & reg_sample==1
	estimates store m2
	suest m1 m2, cluster(b_classid)
	test [m1_mean]biat_ethnic=[m2_mean]biat_ethnic
	estadd scalar p1=r(p),  : spec`i'b				  

	local i = `i'+1
	}

	probit d_bsbully biat_ethnic $covar2c i.grade1 i.b_schoolid if refugee==0 & reg_sample==1, cluster(b_classid)
	margins, dydx(biat_ethnic) post
	estimates store spec9a
	probit d_bsbully biat_ethnic $covar2c i.grade1 i.b_schoolid if refugee==1 & reg_sample==1, cluster(b_classid)
	margins, dydx(biat_ethnic) post
	estimates store spec9b
	probit d_bsbully biat_ethnic $covar2c i.grade1 i.b_schoolid if refugee==0 & reg_sample==1
	estimates store m1
	probit d_bsbully biat_ethnic $covar2c i.grade1 i.b_schoolid if refugee==1 & reg_sample==1
	estimates store m2
	suest m1 m2, cluster(b_classid)
	test [m1_d_bsbully]biat_ethnic=[m2_d_bsbully]biat_ethnic
	estadd scalar p1=r(p),  : spec9b


		#delimit ;
		esttab spec1a spec1b spec2a spec2b spec3a spec3b spec4a spec4b spec5a spec5b
		using "$path2/appendix_tableA4a.tex",  
		keep (biat_ethnic)
        coeflabels(biat_ethnic "Teacher Ethnic Bias")
		replace compress b(%8.2f) se(2)  noconstant nonumbers nobaselevels		
		
		star(* 0.10 ** 0.05 *** 0.01) nonotes 
		s(N p1, fmt(%9.0g %9.3f) labels("N" "P-Value (Host=Refugee)") layout(@ "\multicolumn{2}{S}{@}"))
		nogaps		
		mtitles ( "Host" "Refugee" "Host" "Refugee" "Host" "Refugee" "Host" "Refugee" "Host" "Refugee")
		mgroups("Friendship" "E.S. Received" "E.S. Provided" "A.S. Received" "A.S. Provided", pattern(1 0 1 0 1 0 1 0 1 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
		substitute("&\multicolumn{2}{S}{" "\multicolumn{2}{c}{")
		;
		#delimit cr	

		
		#delimit ;
		esttab spec6a spec6b spec7a spec7b spec9a spec9b spec8a spec8b  
		using "$path2/appendix_tableA4b.tex",  
		keep (biat_ethnic)
        coeflabels(biat_ethnic "Teacher Ethnic Bias")
		replace compress b(%8.2f) se(2)  noconstant nonumbers nobaselevels	
	
		star(* 0.10 ** 0.05 *** 0.01) nonotes 
		s(N p1, fmt(%9.0g %9.3f) labels("N" "P-Value (Host=Refugee)") layout(@ "\multicolumn{2}{S}{@}"))
		nogaps		
		mtitles ( "Host" "Refugee" "Host" "Refugee" "Host" "Refugee" "Host" "Refugee")
		mgroups("Turkish" "Mathematics" "Probability Bullied" "Number of  Bullies", pattern(1 0 1 0 1 0 1 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
		substitute("&\multicolumn{2}{S}{" "\multicolumn{2}{c}{")
		;
		#delimit cr	
		
		


			
******************************************************
*** TRUNCATED REGRESSION
*******************************************************				


	** NUMBER OF OUT-DEGREE HOST TO REFUGEE (FRIEND)
	
	truncreg bfriend_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1 , cluster(b_classid) ul(3)
	estimates store spec1

	** NUMBER OF OUT-DEGREE HOST TO HOST
	
	truncreg bfriend_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid) ul(3)
	estimates store spec2

	** NUMBER OF OUT-DEGREE REFUGEE TO REFUGEE
	
	truncreg bfriend_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1 , cluster(b_classid) ul(3)
	estimates store spec3

	** NUMBER OF OUT-DEGREE REFUGEE TO HOST
	
	truncreg bfriend_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid) ul(3)
	estimates store spec4

	** NUMBER OF OUT-DEGREE HOST TO REFUGEE (SUPPORT)
	
	truncreg bsupport_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1 , cluster(b_classid) ul(3)
	estimates store spec5
	
	** NUMBER OF OUT-DEGREE HOST TO HOST
	
	truncreg bsupport_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid) ul(3)
	estimates store spec6

	** NUMBER OF OUT-DEGREE REFUGEE TO REFUGEE
	
	truncreg bsupport_self_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1 , cluster(b_classid) ul(3)
	estimates store spec7

	** NUMBER OF OUT-DEGREE REFUGEE TO HOST  
	
	truncreg bsupport_self_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid) ul(3)
	estimates store spec8

	** NUMBER OF OUT-DEGREE HOST TO REFUGEE (STUDY)
	
	truncreg bstudy_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1 , cluster(b_classid) ul(3)
	estimates store spec9

	** NUMBER OF OUT-DEGREE HOST TO HOST
	
	truncreg bstudy_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==0 & sample1==1, cluster(b_classid) ul(3)
	estimates store spec10

	** NUMBER OF OUT-DEGREE REFUGEE TO REFUGEE
	
	truncreg bstudy_self_total_refugee biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid) ul(3)
	estimates store spec11

	** NUMBER OF OUT-DEGREE REFUGEE TO HOST 
	
	truncreg bstudy_self_total_host biat_ethnic $covar1a i.grade1 i.b_schoolid if refugee==1 & sample1==1, cluster(b_classid) ul(3)
	estimates store spec12
	
 	
	#delimit ;
		esttab spec1 spec2 spec3 spec4  using "$path2/appendix_tableA3.tex", replace fragment label compress b(%8.2f) se(2) 
		nonumbers keep (biat_ethnic)
		coeflabels(biat_ethnic "\textbf{Panel 1:} Friendship Ties \\ Teacher Ethnic Bias")
		star(* 0.10 ** 0.05 *** 0.01) nonotes noobs nogaps eqlabels(none)
		mtitles ( "Host $\rightarrow$ Refugee" "Host $\rightarrow$ Host" "Refugee $\rightarrow$ Refugee" "Refugee $\rightarrow$ Host")
		mgroups("Host Nominations" "Refugee Nominations" , pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
		substitute(_ \_ \hline\hline \hline)
	;
	#delimit cr

	#delimit ;
		esttab spec5 spec6 spec7 spec8  using "$path2/appendix_tableA3.tex", fragment append label compress b(%8.2f) se(2) 
		nonumbers keep (biat_ethnic)
		coeflabels(biat_ethnic "\textbf{Panel 2:} Emotional Support Ties \\ Teacher Ethnic Bias")
		star(* 0.10 ** 0.05 *** 0.01) nonotes noobs nogaps eqlabels(none)
		nomtitles
		substitute(_ \_ \hline\hline \hline)
	;
	#delimit cr

	#delimit ;
		esttab spec9 spec10 spec11 spec12  using "$path2/appendix_tableA3.tex", fragment append label compress b(%8.2f) se(2) 
		nonumbers keep (biat_ethnic)
		coeflabels(biat_ethnic "\textbf{Panel 3:} Academic Support Ties \\ Teacher Ethnic Bias")
		star(* 0.10 ** 0.05 *** 0.01) nonotes /*noobs*/ nogaps eqlabels(none)
		nomtitles
		substitute(_ \_ \hline\hline \hline) 
	;
	#delimit cr

	est clear
